package serviceImpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import DAO.BaseDAO;
import DTO.Admin;
import pageUtil.PageB;
import util.DBConnection;
import util.DBConnection.CallBack;

public class AdminDAO implements BaseDAO<Admin> {

    /**
     * 管理员添加
     * jd：权限等级
     * active：是否激活标识，若为0则表示未激活，1为激活
     */
    @Override
    public boolean add(Admin t,String sql) throws Exception {

        Connection conn = DBConnection.getConn();
        boolean boo = DBConnection.executUpadte(conn, sql,
                t.getId(),
                t.getUsername(),
                t.getPassword(),
                t.getPhoto(),
                t.getRegtime()
        );
        if(conn!=null){
            conn.close();
        }
        return boo;
    }

    @Override
    public boolean delete(Admin t) throws Exception {

        return false;
    }

    @Override
    public boolean update(Admin t,String sql) throws Exception {

        Connection conn = DBConnection.getConn();
        boolean boo = DBConnection.executUpadte(conn, sql,t.getPassword(),t.getId());
        if(conn!=null){
            conn.close();
        }
        return boo;
    }

    @Override
    public List<Admin> findAll(int currentPage, int pageSize,String sql) throws Exception {
        return null;

    }

    /**
     * 这个方法用来查询所有的管理员信息
     * 条件是必须是active=1的情况才可以进行操作权限
     * active=1表示的就是管理员权限已经被激活
     */
    @Override
    public List<Admin> findAll(String sql) throws Exception {
        sql = "select id,username,password,photo,regtime,jd,active from admin where active=1";
        return DBConnection.queryList(new CallBack<Admin>() {

            @Override
            public List<Admin> getDatas(ResultSet rs) {
                List<Admin> list = new ArrayList<Admin>();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Admin admin = null;
                try {
                    while(rs.next()){
                        String id = rs.getString("id");
                        String username = rs.getString("username");
                        String password = rs.getString("password");
                        String photo = rs.getString("photo");
                        Date regtime = rs.getTimestamp("regtime");
                        int jd = rs.getInt("jd");
                        int active = rs.getInt("active");
                        admin = new Admin(id, username, password, photo, sdf.format(regtime), jd, active);
                        list.add(admin);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    if(rs != null){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }


                return list;
            }


        }, sql);
    }

    @Override
    public Admin findById(String id) throws Exception {
        String sql = "select id,username,password,photo,regtime,jd,active from admin where id=?";
        return DBConnection.queryOne(new CallBack<Admin>() {
            @Override
            public Admin getData(ResultSet rs) {
                Admin admin = null;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    if(rs.next()){
                        String id = rs.getString("id");
                        String username = rs.getString("username");
                        String password = rs.getString("password");
                        String photo = rs.getString("photo");
                        Date regtime = rs.getTimestamp("regtime");
                        int jd = rs.getInt("jd");
                        int active = rs.getInt("active");
                        admin = new Admin(id, username, password, photo, sdf.format(regtime), jd, active);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    if(rs != null){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }

                return admin;
            }


        }, sql, id);
    }

    @Override
    public Admin findByPropert(Admin t) throws Exception {
        String sql = "select id,username,password,photo,regtime,jd,active from admin where username=? and active=1";
        return DBConnection.queryOne(new CallBack<Admin>() {
            @Override
            public Admin getData(ResultSet rs) {
                Admin admin = null;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    if(rs.next()){
                        String id = rs.getString("id");
                        String username = rs.getString("username");
                        String password = rs.getString("password");
                        String photo = rs.getString("photo");
                        Date regtime = rs.getTimestamp("regtime");
                        int jd = rs.getInt("jd");
                        int active = rs.getInt("active");
                        admin = new Admin(id, username, password, photo, sdf.format(regtime), jd, active);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    if(rs != null){
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }

                return admin;
            }


        }, sql, t.getUsername());
    }

    @Override
    public int dataCount(String sql) throws Exception {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public List<Admin> findByPage(PageB<Admin> bean, String sql) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }



}
